package com.lu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lu.constants.HttpStatusEnum;
import com.lu.dto.AjaxPutPage;
import com.lu.dto.AjaxResultPage;
import com.lu.dto.Result;
import com.lu.pojo.BlogComment;
import com.lu.service.BlogCommentService;
import com.lu.utils.DateUtils;
import com.lu.utils.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.awt.*;

/**
 * <p>
 * 评论信息表 前端控制器
 * </p>
 *
 * @author xiaolu
 * @since 2020-05-31
 */
@Controller
@RequestMapping("/admin")
public class CommentController {

    @Resource
    private BlogCommentService blogCommentService;

    // 跳转到评论列表页面
    @GetMapping("/v1/comment")
    public String comment(){

        return "admin/comment-list";
    }

    // 返回评论列表信息
    @GetMapping("/v1/comment/paging")
    @ResponseBody
    public AjaxResultPage<BlogComment> paging(AjaxPutPage<BlogComment> ajaxPutPage, BlogComment condition){

        QueryWrapper<BlogComment> queryWrapper = new QueryWrapper<>(condition);
        Page<BlogComment> page = ajaxPutPage.putPageToPage();
        blogCommentService.page(page,queryWrapper);
        AjaxResultPage<BlogComment> result = new AjaxResultPage<>();
        result.setData(page.getRecords());
        result.setCount(page.getTotal());
        return result;
    }

    // 修改评论状态
    @PostMapping(value = {"/v1/comment/isDel","/v1/comment/commentStatus"})
    @ResponseBody
    public Result<String> modify(BlogComment blogComment){

        boolean flag = blogCommentService.updateById(blogComment);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
    }

    // 编辑评论
    @PostMapping("/v1/comment/edit")
    @ResponseBody
    public Result<String> edit(BlogComment blogComment){

        // 重新设置更新时间
        blogComment.setCommentCreateTime(DateUtils.getLocalCurrentDate());
        boolean flag = blogCommentService.updateById(blogComment);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKK);
        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
    }

    // 删除评论
    @PostMapping("/v1/comment/delete")
    @ResponseBody
    public Result<String> delete(@RequestParam Long commentId){

        boolean flag = blogCommentService.removeById(commentId);
        if (flag){
            return ResultGenerator.getResultByHttp(HttpStatusEnum.OKKK);
        }
        return ResultGenerator.getResultByHttp(HttpStatusEnum.INTERNAL_SERVER_ERROR);
    }

}

